﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BamiTech.AirServer
{
    class HTTPLikeParser
    {
        private HTTPLikeRequest currentHTTPLikeRequest = null;
        public List<HTTPLikeRequest> Parse(byte[] buffer)
        {
            List<HTTPLikeRequest> requests = new List<HTTPLikeRequest>();
            if (currentHTTPLikeRequest == null)
            {
                currentHTTPLikeRequest = new HTTPLikeRequest();
            }
            int used_len = currentHTTPLikeRequest.Parse(buffer);
            if ((used_len == 0) || (used_len > buffer.Length))
            {
                throw new InvalidOperationException();
            }
            if (currentHTTPLikeRequest.Finished())
            {
                requests.Add(currentHTTPLikeRequest);
                currentHTTPLikeRequest = null;
            }
            if (used_len < buffer.Length)
            {
                requests.AddRange(Parse(buffer.Skip(used_len).ToArray()));
            }
            return requests;
        }

    }
}
